﻿function Corey_Page(api_name, template_id, dom_id, initi_data) {

    this.initList = function (page_index, handle) {

        initi_data.page = page_index;

        $.ajax({
            cache: false,
            async: false,
            url: '/api/CommonApi?API=' + api_name,
            type: 'get',
            data: initi_data,
            dataType: 'json',
            success: function (data) {
                if (data.Success) {
                    $('#' + dom_id).html(template(template_id, data));
                    var page_info = data.ApiParamObj[1][0];
                    Init_Page(page_index, page_info.totalRow, page_info.totalPage);
                    if (handle != undefined) {
                        handle.BindingCompleteAfter();
                    }
                }
            }
        });
    }

    function Init_Page(page_index, totalRow, totalPage) {
        if (totalPage > 1) {
            var start_index = (page_index / 10) < 1 ? 1 : page_index % 10 == 0 ? (page_index / 10) > 1 ? page_index - 9 : 1 : parseInt((page_index / 10)) * 10 + 1;
            var min_index = start_index % 10 == 0 ? start_index : start_index > 1 ? start_index + 9 : 10;
            var max_index = totalPage > min_index ? min_index : min_index - 10 + (totalPage % 10);

            var page_html = '<div class="margin-top-40 margin-bottom-40 clearfix">';
            page_html += '      <div>记录总数:<span id="page-count">' + totalRow + '</span></div>';
            page_html += '         <div class="page inline-block float-right">';
            page_html += '             <a href="javascript:void(0);">&lt;&lt;</a>';
            page_html += '             <a href="javascript:void(0);">&lt;</a>';
            for (var i = start_index; i <= max_index; i++) {
                if (i == page_index) {
                    page_html += '             <a class="active" href="javascript:void(0);">' + i + '</a>';
                } else {
                    page_html += '             <a href="javascript:void(0);">' + i + '</a>';
                }
            }
            page_html += '             <a href="javascript:void(0);">&gt;</a>';
            page_html += '             <a href="javascript:void(0);">&gt;&gt;</a>';
            page_html += '         </div>';
            page_html += '     <input type="hidden" id="page-index" value="' + page_index + '" />';
            page_html += '     </div>';

            $('#page-info').html(page_html);

            $('#page-info').find('a').click(function () {
                var $this_index = $(this).html();
                var page_index = $('#page-index').val();
                if (isNaN($this_index) && $this_index == '&lt;' && page_index > 1) {
                    page_index--;
                } else if (isNaN($this_index) && $this_index == '&gt;' && page_index < totalPage) {
                    page_index++;
                } else if (isNaN($this_index) && $this_index == '&lt;&lt;') {
                    page_index = 1;
                } else if (isNaN($this_index) && $this_index == '&gt;&gt;') {
                    page_index = totalPage;
                } else if (!isNaN($this_index)) {
                    page_index = $this_index;
                }
                page.initList(parseInt(page_index));
            }
            );
        } else {
            $('#page-info').html('');
        }
    }

}



function Corey_Page2(api_name, template_id, dom_id, initi_data) {

    this.initList = function (page_index, isadd) {

        initi_data.page = page_index;

        $.ajax({
            cache: false,
            async: false,
            url: '/api/CommonApi?API=' + api_name,
            type: 'get',
            data: initi_data,
            dataType: 'json',
            success: function (data) {
                if (data.Success) {
                    if (isadd) {
                        $('#' + dom_id).append(template(template_id, data));
                    } else {
                        $('#' + dom_id).html(template(template_id, data));
                    }
                }
            }
        });
    }
}